from common import *
import os
import zipfile
from loguru import logger
import hashlib
import json
from threading import Thread
from queue import Queue
import time

class Producer(Thread):
    def __init__(self,name_queue):
        super().__init__()
        self.name_queue = name_queue
        self.father_path = 'E:\\project\\DataBaseData\\DbAnalyze'


    def run(self):
        while True:
            name_list = os.listdir(self.father_path)
            for name in name_list:
                self.name_queue.put(name)
            time.sleep(120)

class BackHandle(Thread):
    def __init__(self,name_queue):
        super().__init__()
        self.name_queue = name_queue
        self.father_path = 'E:\\project\\DataBaseData\\DbAnalyze'
        self.father_path_yesanalyze = 'E:\\project\\DataBaseData\\YesAnalyze'

    def pack(self,fl_datalist):
        lujin = self.father_path_yesanalyze
        json_name = {}
        for i in fl_datalist:
            for n in fl_datalist[i]:
                if n['type'] not in (1, 2, 3, 4, 5):
                    n['type'] = 0
                data_type = str(n['type'])
                folder = lujin + '\\' + data_type + '\\' +str(n['data_source_id'])+'\\'+ i
                if not os.path.exists(folder):
                    os.makedirs(folder)
                with open(folder + '\\' + n['json_name'], 'w') as fh:
                    fh.write(n['text'])
                json_name = {'json_name':n['json_name'],'route':folder + '\\' + n['json_name'],'zip_name':folder + '\\' + n['zip_name']}
        return json_name

    def compress(self,fl_datalist):
        try:
            for i in fl_datalist:
                zip = zipfile.ZipFile(i, 'w', zipfile.ZIP_DEFLATED)
                for n in fl_datalist[i]:
                    zip.write(n['route'], n['json_name'])
                    os.remove(n['route'])
                zip.close()
            return False
        except Exception as e:
            logger.debug('compress wrong ,the reason is {0}'.format(str(e)))
            return True

    def run(self):
        name_queue = self.name_queue
        while True:
            name = name_queue.get()
            zip_path = os.path.join(self.father_path,name)
            zip_key = iscompletezip(zip_path)
            if zip_key:
                continue
            name_readzip = Readzip(zip_path)

            route_list = []
            for json_data in name_readzip:
                fl_datalist = {}
                table_name = json_data['table_name']
                data_source_id = json_data['data_source_id']
                title = json_data['title']
                url = json_data['url']
                title_date = json_data['titleDate']
                data_type = json_data['data_type']
                identify = hashlib.md5((title + url + title_date).encode('utf-8')).hexdigest()

                fl_datalist[title_date] = []
                fl_datalist[title_date].append(
                        {'titleDate': title_date, 'data_source_id': data_source_id, 'zip_name': name,
                         'json_name': identify+'.json', 'text':json.dumps(json_data),
                         'type': data_type})
                route_list.append(self.pack(fl_datalist))
                fl_datalist = {}
            route_datalist = {}
            if len(route_list) > 0:
                for i in route_list:
                    if i['zip_name'] not in route_datalist:
                        route_datalist[i['zip_name']] = []
                    route_datalist[i['zip_name']].append({'json_name': i['json_name'], 'route': i['route']})
            key = self.compress(route_datalist)
            if key:
                continue
            if os.path.exists(os.path.join(self.father_path, name)):
                os.remove(os.path.join(self.father_path, name))
            logger.info('rename success ,the file is {0}'.format(name))


def main():
    name_queue = Queue()
    for i in range(1):
        t = Producer(name_queue)
        t.start()
    for i in range(1):
        t = BackHandle(name_queue)
        t.start()

if __name__ == '__main__':
    main()